#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "DevicesADC.h"
#include "DevicesRT8092.h"
#include "DriverVoltage.h"


static VoltageInfoType st_typeVoltageInfo = {0};


void vVoltageInit(void)
{
    vRT8092Init();

    st_typeVoltageInfo.vdd1.set = 1.8f;
    st_typeVoltageInfo.vdd2H.set = 1.05f;
    st_typeVoltageInfo.vdd2L.set = 1.05f;
    st_typeVoltageInfo.vddQ.set = 0.5f;

    st_typeVoltageInfo.vdd1.limitMax = 2.0f;
    st_typeVoltageInfo.vdd2H.limitMax = 2.0f;
    st_typeVoltageInfo.vdd2L.limitMax = 2.0f;
    st_typeVoltageInfo.vddQ.limitMax = 2.0f;

    st_typeVoltageInfo.vdd1.limitMin = 0.5f;
    st_typeVoltageInfo.vdd2H.limitMin = 0.5f;
    st_typeVoltageInfo.vdd2L.limitMin = 0.5f;
    st_typeVoltageInfo.vddQ.limitMin = 0.5f;

    cVoltageSet(VOLTAGE_CHANNEL_ALL, 0.0f);
}

int8_t cVoltageSet(uint32_t uiChannel, float fVoltage)
{
    int8_t cError = 0;

    if(uiChannel & VOLTAGE_CHANNEL_VDD1)
    {
        cError |= cRT8092ValueSet(RT8092_CHANNEL_VDD1, fVoltage);
        st_typeVoltageInfo.vdd1.set = fVoltage;
    }

    if(uiChannel & VOLTAGE_CHANNEL_VDD2H)
    {
        cError |= cRT8092ValueSet(RT8092_CHANNEL_VDD2H, fVoltage);
        st_typeVoltageInfo.vdd2H.set = fVoltage;
    }

    if(uiChannel & VOLTAGE_CHANNEL_VDD2L)
    {
        cError |= cRT8092ValueSet(RT8092_CHANNEL_VDD2L, fVoltage);
        st_typeVoltageInfo.vdd2L.set = fVoltage;
    }

    if(uiChannel & VOLTAGE_CHANNEL_VDDQ)
    {
        cError |= cRT8092ValueSet(RT8092_CHANNEL_VDDQ, fVoltage);
        st_typeVoltageInfo.vddQ.set = fVoltage;
    }

    return cError;
}

float fVoltageGet(uint32_t uiChannel)
{
    float fVoltage = 0;

    switch(uiChannel)
    {
        case VOLTAGE_CHANNEL_VDD1  : fVoltage = fADCGetValue(ADC_CHANNEL_VOLTAGE_VCC1);  break;
        case VOLTAGE_CHANNEL_VDD2H : fVoltage = fADCGetValue(ADC_CHANNEL_VOLTAGE_VCC2H); break;
        case VOLTAGE_CHANNEL_VDD2L : fVoltage = fADCGetValue(ADC_CHANNEL_VOLTAGE_VCC2L); break;
        case VOLTAGE_CHANNEL_VDDQ  : fVoltage = fADCGetValue(ADC_CHANNEL_VOLTAGE_VCCQ);  break;

        default : break;
    }

    return fVoltage;
}

void vVoltageUpdate(void)
{
    st_typeVoltageInfo.vdd1.get  = fVoltageGet(VOLTAGE_CHANNEL_VDD1);
    st_typeVoltageInfo.vdd1.getFilter = (st_typeVoltageInfo.vdd1.get * 0.1f) + (st_typeVoltageInfo.vdd1.getFilter * 0.9f);

    st_typeVoltageInfo.vdd2H.get = fVoltageGet(VOLTAGE_CHANNEL_VDD2H);
    st_typeVoltageInfo.vdd2H.getFilter = (st_typeVoltageInfo.vdd2H.get * 0.1f) + (st_typeVoltageInfo.vdd2H.getFilter * 0.9f);

    st_typeVoltageInfo.vdd2L.get = fVoltageGet(VOLTAGE_CHANNEL_VDD2L);
    st_typeVoltageInfo.vdd2L.getFilter = (st_typeVoltageInfo.vdd2L.get * 0.1f) + (st_typeVoltageInfo.vdd2L.getFilter * 0.9f);

    st_typeVoltageInfo.vddQ.get  = fVoltageGet(VOLTAGE_CHANNEL_VDDQ);
    st_typeVoltageInfo.vddQ.getFilter = (st_typeVoltageInfo.vddQ.get * 0.1f) + (st_typeVoltageInfo.vddQ.getFilter * 0.9f);
}

VoltageInfoType *ptypeVoltageInfoGet(void)
{
    return &st_typeVoltageInfo;
}
